<?php
if (! defined ( 'BASEPATH' ))
    exit ( 'No direct script access allowed' );

class Rekhistory_model extends Model {

    protected $_table = 'RekHistory';
    protected $_prefix = 'rekHistory';
    
    public function __construct() {
        // Call the Model constructor
        parent::__construct ();
    }

    public function add($data) {
        if (! $data || ! is_array ( $data ))
            return false;
        return $this->db->insert ( $this->_table, $data ) && ($this->db->affected_rows () > 0) ? true : false;
    
    }
    public function update($id, $data) {
        if (! $id || ! $this->get ( $id ) || ! $data)
            return array ();

        $this->db->where ( 'rekHistoryId', $id );
        $this->db->limit ( 1 );
        return $this->db->update ( $this->_table, $data ) && ($this->db->affected_rows () > 0) ? true : false;
    }
	public function delete($id) {
        if (! $id || ! $this->get ( $id ))
            return array ();
        $this->db->where ( 'rekHistoryId', $id );
        $this->db->limit ( 1 );
        return $this->db->delete ( $this->_table ) && ($this->db->affected_rows () > 0) ? true : false;
    }
    
    public function get($id) {
        if (! $id) 
            return array ();
        
        $this->db->select ( 'RekHistory.*' );
        $this->db->select ( 'Rekening.rekTipeId, Rekening.rekeningNama' );
        $this->db->select ( 'RekTipe.rekTipe, RekTipe.rekTipeNama' );
        
    	$this->db->from ( $this->_table );
        $this->db->join ( 'Rekening', 'RekHistory.rekeningId = Rekening.rekeningId' );
        $this->db->join ( 'RekTipe', 'Rekening.rekTipeId = RekTipe.rekTipeId' );
        
        $this->db->where ( 'RekHistory.rekHistoryId', $id);
        $this->db->limit ( 1 );	
        $query = $this->db->get ();
        return $query->row_array ();
    }
    
    public function getList($limit = 100, $offset = 0, $where=array()) { 
        $this->db->select ( 'RekHistory.*' );
        $this->db->select ( 'Rekening.rekTipeId, Rekening.rekeningNama' );
        $this->db->select ( 'RekTipe.rekTipe, RekTipe.rekTipeNama' );
    	$this->db->from ( $this->_table );
        $this->db->join ( 'Rekening', 'RekHistory.rekeningId = Rekening.rekeningId' );
		$this->db->join ( 'RekTipe', 'Rekening.rekTipeId = RekTipe.rekTipeId' );
		
        if(isset($where['rekTipeId']))
        	$this->db->where('Rekening.rekTipeId', $where['rekTipeId']);
		if(isset($where['historyPeriodeTahun']))
        	$this->db->where('RekHistory.historyPeriodeTahun', $where['historyPeriodeTahun']);	
        if(isset($where['historyPeriodeBulan']))
        	$this->db->where('RekHistory.historyPeriodeBulan', $where['historyPeriodeBulan']);	

        $this->db->order_by ( 'RekHistory.historyPeriodeTahun', 'desc' );
        $this->db->order_by ( 'RekHistory.historyPeriodeBulan', 'desc' );	
        $this->db->order_by ( 'Rekening.rekTipeId', 'asc' );	
        $this->db->order_by ( 'Rekening.rekeningId', 'asc');
        $this->db->limit ( $limit, $offset );
        $query = $this->db->get ();
        return $query->result_array ();
    }
    public function getCount($where=array()) {
        $this->db->select ( 'count(RekHistory.rekeningId) as count' );
        $this->db->from ( $this->_table );
        $this->db->join ( 'Rekening', 'RekHistory.rekeningId = Rekening.rekeningId' );
        
        if(isset($where['rekTipeId']))
        	$this->db->where('Rekening.rekTipeId', $where['rekTipeId']);
        if(isset($where['historyPeriodeTahun']))
        	$this->db->where('RekHistory.historyPeriodeTahun', $where['historyPeriodeTahun']);	
        if(isset($where['historyPeriodeBulan']))
        	$this->db->where('RekHistory.historyPeriodeBulan', $where['historyPeriodeBulan']);
        	
        $query = $this->db->get ();
        if ($query && ($row = $query->row_array ())) {
            return $row ['count'];
        }
        return 0;
    }
    
}